const api = require("./../../config/api")
const request = require("./../../utils/request")
const app = getApp()

Page({
	data: {
		active: 0,
		page:1,
		articleSortList: [],
		articleList: [],
		height: '',
		current: 1,
		pageSize: 5,
		page: 1,
		size: 5
	},
	// 事件处理函数
	tabClick(val) {
		this.setData({
			active: val.currentTarget.dataset.tab
		})
		this.setData({
			articleList:[]
		})
		this.getArticleList(val.currentTarget.dataset.tab)
	},
	articleDetial(val) {
		const item = val.currentTarget.dataset.item
		wx.navigateTo({
			url:'../article-detail/index?id='+item.id
		})
	},
	onLoad() {
		this.setData({
			height: wx.getSystemInfoSync().windowHeight+'px'
		})
		this.getArticleSortList()
	},
	// 获取文章分类
	getArticleSortList: async function() {
		const res = await request.get(api.getArticleSortList)
		if(res.httpStatus === 200){
			this.setData({
				articleSortList:res.data,
				active:res.data[0].id
			})
			this.getArticleList(res.data[0].id)
		}
	},
	// 获取文章列表
	getArticleList: async function(id) {
		const data = {
			dictId:id,
			current: this.data.current,
			pageSize:this.data.pageSize,
			page: this.data.page,
			size:this.data.size
		}
		const res = await request.get(api.getArticleList, data)
		let newList = this.data.articleList
		
		if(res.httpStatus === 200){
			if(res.data.records.length === 0){
				wx.showToast({
				  title: '没有了',
				  icon: "none",
				})
				return
			}
			newList.push(...res.data.records)
			this.setData({
				articleList: newList
			})
		}
	},
	//监听是否滑到底部
	nextPage: function() {
		let new_lists = [];
		let page = this.data.page;
		page += 1;
		this.setData({
			page:page
		})
		this.getArticleList(this.data.active)
	}
})
